package com.oaec.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.oaec.po.User;

/**
 * Servlet implementation class TestServlet
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        System.out.println("我的构造方法被调用了");
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		String creater=(String)config.getInitParameter("creater");

		System.out.println("初始化servlet,得到配置参数creater为："+creater);
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		System.out.println("销毁servlet");
	}

	/**
	 * @see Servlet#getServletConfig()
	 */
	public ServletConfig getServletConfig() {
		System.out.println("获得servlet的配置项");
		return null;
	}

	/**
	 * @see Servlet#getServletInfo()
	 */
	public String getServletInfo() {
		System.out.println("获得servlet的信息");
		return null; 
	}
	

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("我正在提供服务，来自于get方式的请求");
		response.setCharacterEncoding("UTF-8");//告诉服务端输出流，说你要以utf-8输出以下内容
		response.setContentType("text/html; charset=UTF-8");
		
		String user_name = request.getParameter("user_name");//用户名
		String user_pass = request.getParameter("user_pass");//密码
		
		request.setAttribute("name", user_name);
		request.setAttribute("pass", user_pass);
		
		HttpSession session = request.getSession();
		session.setAttribute("s_name", user_name);
		session.setAttribute("s_pass", user_pass);
		
		ServletContext appliction = request.getServletContext();
		appliction.setAttribute("a_name", user_name);
		appliction.setAttribute("a_pass", user_pass);
		
		List<User> userList=new ArrayList<User>();
		for(int i=0;i<10;i++){
			userList.add(new User("a"+i,"p"+i));
		}
		session.setAttribute("list", userList);
		
		//转到成功页面
		request.getRequestDispatcher("/success.jsp").forward(request, response);
		
		/*PrintWriter out = response.getWriter();
		out.append("来自于get方式的请求").append("<br>");
		out.append("登录账号为: ").append(user_name).append("<br>");
		out.append("登录密码为: ").append(user_pass).append("<br>");*/
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("我正在提供服务，来自于pot方式的请求");
		this.doGet(request, response);
	}

}
